package com.customization.unionpay.market.util;

import com.alibaba.fastjson.JSONObject;
import com.weaver.formmodel.util.EncryptHelper;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author sjm
 * @description 预算管理相关
 */
public class BudgetManagementUtils {

    private static final Logger log = LoggerFactory.getLogger(BudgetManagementUtils.class);

    /**
     * 预算校验
     * @param barcode
     * @param useraccount
     * @param params
     * @return
     */
    public static String budgetVerify(String barcode,String useraccount, List<JSONObject> params) throws Exception {
        String budgetVerifyUrl = com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "budget_verify_url");
        log.info("预算校验Url:"+budgetVerifyUrl);
        JSONObject safety = getSafety(barcode, useraccount);
        log.info("安全验证参数:"+safety);

        JSONObject param = new JSONObject();
        param.put("safety",safety);
        param.put("params",params);
        log.info("预算校验整体参数:"+param);
        return HttpUtils.doPost(budgetVerifyUrl,null,param,null,null);
    }

    /**
     * 预算执行
     * @param barcode
     * @param useraccount
     * @param params
     * @return
     */
    public static String budgetExecBudget(String barcode, String useraccount, List<JSONObject> params) throws Exception {
        String budgetExecBudgetUrl = com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "budget_execBudget_url");
        log.info("预算执行Url:"+budgetExecBudgetUrl);
        JSONObject safety = getSafety(barcode, useraccount);
        log.info("安全验证参数:"+safety);

        JSONObject param = new JSONObject();
        param.put("safety",safety);
        param.put("params",params);
        log.info("预算执行整体参数:"+param);
        return HttpUtils.doPost(budgetExecBudgetUrl,null,param,null,null);
    }


    /**
     * 组合安全验证参数
     * @param barcode           操作单号
     * @param useraccount       操作人账号
     * @return
     * @throws Exception
     */
    public static JSONObject getSafety(String barcode,String useraccount) {

        JSONObject safety = new JSONObject();
        String timeStr = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
        String clientcode = com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "clientcode");
        String safetypwd = PropUtils.BB_LOG.getPropValue("marketSubject", "safetypwd");
        String key = timeStr+clientcode+barcode+useraccount+safetypwd;
        log.info("key:"+key);
        // ticket  安全校验位，Time的值+clientcode的值+barcode的值+useraccount的值+密码, 整体生成MD5; 密码：zhcwptimage
        String md5Str = getMd5Str(key);
        // time  时间	yyyyMMddhhmmss
        safety.put("time",timeStr);
        safety.put("ticket",md5Str);
        // clientcode 电子凭证原文件类型 接口调用方账号ZHCWPT
        safety.put("clientcode",clientcode);
        // barcode	  操作单号
        safety.put("barcode",barcode);
        // useraccount 操作人账号
        safety.put("useraccount",useraccount);
        return safety;
    }

    /**
     * md5加密
     * @param plainText
     * @return
     * @throws Exception
     */
    public static String getMd5Str(String plainText){
        return EncryptHelper.encodeMd5(plainText);
    }

}
